<template>
  <el-table :data="courses" style="width: 100%">
    <el-table-column label="编号" prop="id" />
    <el-table-column label="名称" prop="name" />
    <el-table-column label="学分" prop="point" />
    <el-table-column label="学时" prop="time" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="keyword" size="small" placeholder="搜索课程" />
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)">
          更新
        </el-button>
        <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
        >
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination background layout="prev, pager, next" :total="total"
                 :page-size="pageSize" @current-change="loadCourses"/>

  <el-dialog v-model="dialogFormVisible" title="课程信息" width="500">
    <el-form :model="course">
      <el-form-item label="名称" >
        <el-input v-model="course.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="学分" >
        <el-input v-model="course.point" autocomplete="off" />
      </el-form-item>
      <el-form-item label="学时" >
        <el-input v-model="course.time" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: "CourseView",
  data() {
    return {
      courses: [],
      keyword: '',
      current:1,
      pageSize:10,
      total:10,
      dialogFormVisible:false,
     course: {}
    };
  },
  methods: {
    loadCourses(current) {
      this.current=current
      this.$axios.get('/findCoursePage?current='+this.current +'&pageSize='+this.pageSize)
          .then(result => {
            if (result.data.code === 200) {
              this.courses = result.data.data.data;
              this.total = result.data.data.total;
            }
          })
          .catch(error => {
            console.error("加载课程数据失败:", error);
          });
    },
    handleEdit(row) {
      this.dialogFormVisible = true;
      this.course = JSON.parse(JSON.stringify(row));
      console.log("编辑课程:", row);
    },
    handleDelete(row) {
      // 删除逻辑
      console.log("删除课程:", row);
    }
  },
  mounted() {
    this.loadCourses(1);
  }
};
</script>

<style scoped>
/* 样式可以根据需要自定义 */
</style>
